package com.jyu.apts.utils;

import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

import com.jyu.apts.entity.User;

//加密工具类
public class PasswordEntryUtil {

	    //随机数生成器
	    public static RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();

	    //指定散列算法为md5
	    public static String algorithmName = "MD5";
	    //散列迭代次数
	    public static final int hashIterations = 1;

	    /**
	     * 生成随机盐值对密码进行加密
	     * @param User
	     * @return
	     */
	    public static User encrypt(User user) {
	        user.setSalt(randomNumberGenerator.nextBytes().toHex());
	        String newPassword = new SimpleHash(algorithmName,user.getPassword(),
	        		ByteSource.Util.bytes(user.getSalt()),hashIterations).toHex();

	        user.setPassword(newPassword);
	        return user;

	    }
	    
	    public static void main(String[] args) {
	    	String salt = randomNumberGenerator.nextBytes().toHex();
	    	System.out.println("salt:"+salt);
	    	String newPassword = new SimpleHash(algorithmName,"123456",
	        		ByteSource.Util.bytes(salt),hashIterations).toHex();
	    	System.out.println(newPassword);
		}

}
